package org.dawning.shop.category.service;

import lombok.extern.log4j.Log4j;
import org.dawning.shop.category.entity.ViewCategory;
import org.dawning.shop.category.repertory.ViewCategoryRepository;
import org.dawning.shop.core.base.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Created by L on 2017/6/12.
 */
@Service
@Log4j
public class ViewCategoryService {

    @Autowired
    private ViewCategoryRepository pcCategoryRepository;

    /**
     * @param name     前端分类名
     * @param code     前端分类code
     * @param parentId 父ID
     * @param sort     排序
     * @return
     */
    public BaseResult add(String name, String code, String parentId, int sort, String baseCategory) {
        try {
            ViewCategory pcCategry = new ViewCategory();
            pcCategry.preInsert();
            pcCategry.setBaseCategory(baseCategory);
            pcCategry.setCategoryCode(code);
            pcCategry.setCategoryName(name);
            pcCategry.setParentId(parentId);
            pcCategry.setSort(sort);
            ViewCategory pcCategrys = pcCategoryRepository.save(pcCategry);
            if (pcCategrys != null) {
                return new BaseResult(200, "success", pcCategrys);
            } else {
                return new BaseResult(400, "fail", null);
            }
        } catch (Exception e) {
            log.info("error", e);
            return new BaseResult(500, "error", null);
        }
    }


    /**
     * @param id 前端分类ID
     * @return
     */
    public BaseResult get(String id) {
        try {
            ViewCategory pcCategry = pcCategoryRepository.findOne(id);
            if (pcCategry != null) {
                return new BaseResult(200, "success", pcCategry);
            } else {
                return new BaseResult(400, "fail", null);
            }
        } catch (Exception e) {
            log.info("error", e);
            return new BaseResult(500, "error", null);
        }
    }

    /**
     * @param id 前端分类ID
     * @return
     */
    public BaseResult delete(String id) {
        try {
            ViewCategory pcCategry = pcCategoryRepository.findOneById(id);
            if (pcCategry != null) {
                pcCategry.logicDelete();
                pcCategoryRepository.save(pcCategry);
                return new BaseResult(200, "success", null);
            } else {
                return new BaseResult(400, "fail", null);
            }
        } catch (Exception e) {
            log.info("error", e);
            return new BaseResult(500, "error", null);
        }
    }

    /**
     * @param id       前端分类ID
     * @param name     前端分类名
     * @param code     前端分类code
     * @param parentId 父ID
     * @param sort     排序
     * @return
     */
    public BaseResult update(String id, String name, String code, String parentId, int sort, String baseCategory) {
        try {
            ViewCategory pcCategry = pcCategoryRepository.findOneById(id);
            if (pcCategry == null) {
                return new BaseResult(400, "fail", null);
            }
            if (!StringUtils.isEmpty(code)) {
                pcCategry.setCategoryCode(code);
            }
            if (!StringUtils.isEmpty(name)) {
                pcCategry.setCategoryName(name);
            }
            if (!StringUtils.isEmpty(parentId)) {
                pcCategry.setParentId(parentId);
            }
            if (!StringUtils.isEmpty(baseCategory)) {
                pcCategry.setBaseCategory(baseCategory);
            }
            if (sort > 0) {
                pcCategry.setSort(sort);
            }
            pcCategry.preUpdate();
            ViewCategory pcCategrys = pcCategoryRepository.save(pcCategry);
            if (pcCategrys != null) {
                return new BaseResult(200, "success", pcCategrys);
            } else {
                return new BaseResult(400, "fail", null);
            }
        } catch (Exception e) {
            log.info("error", e);
            return new BaseResult(500, "error", null);
        }

    }
}
